import storejs from 'storejs';
import api from '@/network/api';
import Cookies from 'js-cookie';

import { getMasterCommonPkg } from '@/utils/masterData';
const { getStore, history, ep } = getMasterCommonPkg();
var store = getStore();

const {
  userApi: { login },
} = api;

let initState = () => ({});

export default {
  namespace: 'login',
  state: { ...initState() },
  effects: {
    // 登录
    *login({ payload }, { call, put }) {
      var obj = {
        params: {
          username: payload.username,
          password: payload.password,
        },
      };
      // const rs = yield call(login, obj);
      // 绕过登陆接口
      var rs={
        success:true,
        data:{
          access_token:'123123123asdaswd',
        }
      }
      if (rs.success) {
        // var token = rs.data.token_type + ' ' + rs.data.access_token;
        var token = rs.data.access_token;
        storejs('token', token);
        storejs('autoLogin', payload.autoLogin ? 'true' : 'false');
        Cookies.set('token', token);
        store.dispatch({ type: `user/getUserinfo` });
        history.replace('/');
        ep.emit('basicLayoutInit');
      }
      return Promise.resolve(rs);
    },
  },

  reducers: {
    setState(state, action) {
      return { ...state, ...action.payload };
    },
    init(state, action) {
      return { ...initState(), ...action.payload };
    },
  },
};
